package com.InsideGamer.SuperJailBreak;

import java.io.IOException;
import java.util.HashMap;

import net.minecraft.server.Packet;
import net.minecraft.server.Packet20NamedEntitySpawn;
import net.minecraft.server.Packet29DestroyEntity;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player;

public class Users
{
	private JailBreak plugin;
	private YamlConfiguration users;
	
	private HashMap<String, Boolean> HiddenMode;
	private HashMap<String, Double> Money;
	//private HashMap<String, Boolean> ShowNameTagMode;
	
	public Users ( JailBreak pluginObject, YamlConfiguration users, YamlConfiguration config )
	{
		plugin = pluginObject;
		
		//users = userConfig;
		
		HiddenMode = new HashMap<String, Boolean>();
		Money = new HashMap<String, Double>();
		
		//plugin.onDisable();
	}
	
	public void save ( ) throws IOException
	{
        /* For all users
        for(int i = 0; i < OpNames.size(); i++)
        {
            // Update user
            HashMap<String, Object> map = new HashMap();
            map.put("group", OpGroup.get(i));
            map.put("title", OpTitle.get(i));
            
            // Does this user have kick-time remaining?
            Integer kickTime = KickedTimes.get(OpNames.get(i));
            if(kickTime != null)
                map.put("kicktime", kickTime);
            
            // Was this user banned at some point?
            String banReason = BannedUsers.get(OpNames.get(i));
            if(banReason != null)
                map.put("banned", banReason);
            
            // Save this user to the map
            users.setProperty(OpNames.get(i), map);
        }*/
        
        // Save file
        users.save( "filename" );
		plugin.log.info ( "[SuperJailBreak] All userdata have been saved!" );
	}
	
	public void ShowNameTag ( Player player, String ShowTag )
	{
		player.setDisplayName( ShowTag );
	}
	
	public double GetUserMoney ( Player player )
	{
		return Money.get ( player.getName() );
	}
	
	public void ChangeMoneyTo ( Player player, Player target, String amount )
	{
		try {
		Double amounttochange = new Double ( amount );
		Double playerAmount = Money.get ( player.getName() );
		Double targetAmount = Money.get ( target.getName() );
		
		if ( playerAmount == null )
			Money.put ( player.getName(), 0.00 );
		if ( targetAmount == null )
			Money.put ( target.getName(), 0.00 );
		
		if ( amounttochange < 0.01 )
		{
			player.sendMessage ( "Je kunt geen bedrag lager dan 0.01 overmaken!" );
			return;
		}
				
		if ( amounttochange > playerAmount )
		{
			player.sendMessage("Je hebt niet genoeg geld! Je hebt maar " + playerAmount + "euro." );
			return;
		}
		
		// Edit players money.
		Money.remove( player.getName() );
		Money.put ( player.getName(), playerAmount - amounttochange );
		
		// Edit target players money.
		Money.remove( target.getName() );
		Money.put ( target.getName(), targetAmount + amounttochange );
		
		player.sendMessage ( "Je hebt " + target.getName() + " " + amounttochange + " euro gegeven." );
		target.sendMessage ( "Je hebt " + amounttochange + " ontvangen van " + player.getName() + "." );
		}
		catch ( Exception e)
		{
			plugin.BroadcastMessage(e.getMessage());
		}
	}
	
	public boolean IsHidden ( Player target )
	{
		Boolean isHidden = HiddenMode.get(target.getName());
		
		if ( isHidden == null )
			return false;
		else
			return isHidden.booleanValue();
	}
	
	public void SetHidden ( Player target, boolean IsHidden )
	{
		// Save new hash
		if(IsHidden == false)
			HiddenMode.remove ( target.getName() );
		else
			HiddenMode.put ( target.getName(), IsHidden );
		
		// For each online player, send a "gone" packet
		for ( Player other : plugin.getServer().getOnlinePlayers() )
		{
			// Ignore self
			if ( other != target )
			{
				// Are we hiding the player?
				if ( IsHidden )
				{
					// Cast to get access to send custom packet
					// Target hides from others
					CraftPlayer targetCraftPlayer = (CraftPlayer)target;
					CraftPlayer otherCraftPlayer = (CraftPlayer)other;
					Packet hideTarget = new Packet29DestroyEntity ( targetCraftPlayer.getEntityId() );
					otherCraftPlayer.getHandle().netServerHandler.sendPacket(hideTarget);
				}
				else
				{
					// Cast to get access to send custom packet
					CraftPlayer targetCraftPlayer = (CraftPlayer)target;
					CraftPlayer otherCraftPlayer = (CraftPlayer)other;
					Packet unhideTarget = new Packet20NamedEntitySpawn(targetCraftPlayer.getHandle());
					otherCraftPlayer.getHandle().netServerHandler.sendPacket(unhideTarget); 
				}
			}
		}
		
		if ( IsHidden ( target ) )
		{
			target.sendMessage ( ChatColor.GRAY + "Je bent nu onzichtbaar!" );
		}
		else
		{
			target.sendMessage ( ChatColor.GRAY + "Je bent nu weer zichtbaar!" );
		}
	}
}
